package com.czxy.tanhua.service;

import cn.hutool.core.collection.CollUtil;
import com.czxy.tanhua.dubbo.api.UserInfoApi;
import com.czxy.tanhua.dubbo.mongoapi.RecommendUserApi;
import com.czxy.tanhua.entity.UserInfo;
import com.czxy.tanhua.mongo.RecommendUser;
import com.czxy.tanhua.vo.RecommendPageVo;
import com.czxy.tanhua.vo.ResultList;
import com.czxy.tanhua.vo.UserInfoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/4/7 16:13
 */
@Service
public class RecommendService {
    @DubboReference
    private RecommendUserApi recommendUserApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    public ResultList findAllrecommendation(RecommendPageVo recommendPageVo, Long userId) {

        ResultList allrecommendation = recommendUserApi.findAllrecommendation(recommendPageVo, userId);
        List<RecommendUser> items = (List<RecommendUser>)allrecommendation.getItems();
        //根据ids查询详情
        List<Long> ids = CollUtil.getFieldValues(items, "userId", Long.class);
        //构建条件
        UserInfo userInfo = new UserInfo();
        userInfo.setGender(recommendPageVo.getGender());
        userInfo.setAge(Integer.valueOf(recommendPageVo.getAge()));
        System.out.println(userInfo);
        Map<Long, UserInfo> map = userInfoApi.findByids(ids, userInfo);

        List<UserInfoVo> userInfoList = new ArrayList<>();

        for (RecommendUser item : items) {
            UserInfo userInfo1 = map.get(item.getUserId());
            UserInfoVo userInfoVo = new UserInfoVo();
            if (userInfo1 !=null){
                userInfoVo.setId(userInfo1.getId());
                userInfoVo.setAge(userInfo1.getAge());
                userInfoVo.setAvatar(userInfo1.getAvatar());
                userInfoVo.setGender(userInfo1.getGender());
                userInfoVo.setFateValue(item.getScore().intValue());
                userInfoVo.setNickname(userInfo1.getNickname());
                String[] split = userInfo1.getTags().split(",");
                userInfoVo.setTags(split);
                userInfoList.add(userInfoVo);
            }
        }
        allrecommendation.setItems(userInfoList);
        return allrecommendation;
    }

    public UserInfoVo findtodayBest(Long userId) {
        UserInfoVo userInfoVo = recommendUserApi.findtodayBest(userId);
        return userInfoVo;
    }

    public UserInfoVo findUserInfo(Long id, Long userId) {
        if (id == userId){
            UserInfo user = userInfoApi.findUser(userId);
            UserInfoVo userInfoVo = UserInfoVo.init(user, 100);
            return userInfoVo;
        }
        return recommendUserApi.findUserInfo(id,userId);
    }
}
